package lazy2.tool.beangenerator;

import lazy2.util.StringUtil;

public class JdbcUrl {
	private DBType dbType;
	private String address;
	private String schema;
	private String jdbcUrl;
	
	
	public JdbcUrl(DBType dbType, String address, String schema) {
		if(dbType==null || !StringUtil.hasText(address) || !StringUtil.hasText(schema))
			throw new IllegalArgumentException();
		this.dbType = dbType;
		this.address = address;
		this.schema = schema;
	}
	public DBType getDbType() {
		return dbType;
	}
	public String getAddress() {
		return address;
	}
	public String getSchema() {
		return schema;
	}
	public String getJdbcUrl() {
		if(jdbcUrl==null) {
			jdbcUrl = getJdbcUrl(dbType, address, schema);
		}
		return jdbcUrl;
	}
	public static String getJdbcUrl(DBType dbType, String address, String schema) {
		String url = null;
		switch (dbType) {
			case MySQL:
				url = new StringBuilder("jdbc:mysql://").append(address).append("/").append(schema).toString();
				break;
			case MSSQLServer:
				url = new StringBuilder("jdbc:jtds:sqlserver://").append(address).append("/").append(schema).toString();
				break;
		}
		return url;
	}
}
